<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use think\Db;
use think\Controller;
use think\Request;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Expenditure  extends Backend
{

    /**
     * UserWarehouse模型对象
     */
    protected $model = null;
    protected $warehouse_items;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('UserWallet');
        //$this->warehouse_items = config('user_warehouse');

    }
    /**
     * 悬赏任务
     */
    public function index()
    {
        $status=input('status');
        $this->view->assign('status', $status);
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where = array();

            $filter=input();
            $filters = json_decode($filter['filter'], true);

            if(!empty($filters['user_id'])){
                $where['u.user_id']=$filters['user_id'];
            }

            if(!empty($filters['user_name'])){
                $where['u.user_name']=$filters['user_name'];
            }
            if(!empty($filters['full_name'])){
                $where['u.full_name']=array('like','%'.$filters['full_name'].'%');
            }
            if(!empty($filters['last_time'])) {
                $date = explode(',', $filters['last_time']);
                foreach($date as &$v){
                    $v = strtotime($v);
                }
                $where['u.last_time'] = array('between', $date);
            }
            $field=['a.*,u.user_name,u.full_name'];
            $where['mission_status']=2;
            $total=DB::name('amoy')
                ->alias('a')
                ->join('users u','u.user_id=a.user_id')
                ->where($where)
                ->field($field)
                ->order($sort, $order)
                ->group('a.user_id')
                ->count();
            $amony=DB::name('amoy')
                ->alias('a')
                ->join('users u','u.user_id=a.user_id')
                ->where($where)
                ->field($field)
                ->group('a.user_id')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach($amony as $key=>$a){
                $amony[$key]['count']=DB('amoy')->where('mission_status=2 and user_id='.$a['user_id'])->count();
                $amony[$key]['sum']=DB('amoy')->where('mission_status=2 and user_id='.$a['user_id'])->sum('reward_price');
                $amony[$key]['service_sum']=DB('amoy')->where('mission_status=2 and user_id='.$a['user_id'])->sum('service_price');
                //删除有偿点击退还赏金
                $amony[$key]['click_price']=DB('user_echarge')->where('user_id='.$a['user_id'].' and process_type=8')->value('amount');
            }

            $result = array("total" => $total, "rows" => $amony);


            return json($result);
        }

        $btns = ['refresh','singlesearch'];
        $this->view->assign('btns', $btns);

        return $this->view->fetch();
    }
    //有偿分享
    public function index2(){
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where = array();
            $filter=input();
            $filters = json_decode($filter['filter'], true);
            if(!empty($filters['user_id'])){
                $where['u.user_id']=$filters['user_id'];
            }
            if(!empty($filters['user_name'])){
                $where['u.user_name']=$filters['user_name'];
            }
            if(!empty($filters['full_name'])){
                $where['u.full_name']=array('like','%'.$filters['full_name'].'%');
            }
            $field=array('a.*','c.amoy_id','u.user_name','u.full_name','x.amoy_sn','x.click_amount','x.service_price','x.click_unit_price');
            $total=DB::name('user_echarge')
                ->alias('a')
                ->join('click_money c','c.share_user_id=a.user_id')
                ->join('users u','a.user_id=u.user_id')
                ->join('amoy x','x.amoy_id=c.amoy_id')
                ->field($field)
                ->where($where)
                ->order($sort, $order)
                ->group('a.user_id')
                ->where('a.process_type=2')
                ->count();

            $amony=DB::name('user_echarge')
                ->alias('a')
                ->join('click_money c','c.share_user_id=a.user_id')
                ->join('users u','a.user_id=u.user_id')
                ->join('amoy x','x.amoy_id=c.amoy_id')
                ->field($field)
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->group('a.user_id')
                ->where('a.process_type=2')
                ->select();
            foreach($amony as $key=>$a){
                //支付次数
                $amony[$key]['count']=DB('user_echarge')->where('user_note','like','%有偿点击收入,编号:'.$a['amoy_sn'].'%')->count();
                //付费总额
                $amony[$key]['sum']=$amony[$key]['count']*$a['click_unit_price'];
                //删除有偿点击退还赏金
                $amony[$key]['click_price']=DB('user_echarge')->where('user_id='.$a['user_id'].' and process_type=11')->value('amount');
                $amony[$key]['amount']=($a['amount'])*-1;
            }
            $result = array("total" => $total, "rows" => $amony);

            return json($result);
        }
    }
    //信息支出
    public function index3(){
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where = array();
            $filter=input();
            $filters = json_decode($filter['filter'], true);
            if(!empty($filters['user_id'])){
                $where['u.user_id']=$filters['user_id'];
            }
            if(!empty($filters['user_name'])){
                $where['u.user_name']=$filters['user_name'];
            }
            if(!empty($filters['full_name'])){
                $where['u.full_name']=array('like','%'.$filters['full_name'].'%');
            }
            if(!empty($filters['last_time'])) {
                $date = explode(',', $filters['last_time']);
                foreach($date as &$v){
                    $v = strtotime($v);
                }
                $where['u.last_time'] = array('between', $date);
            }
            $where['process_type']=5;
            $total=DB::name('user_echarge')
                ->alias('a')
                ->join('users u','a.user_id=u.user_id')
                ->where($where)
                ->order($sort, $order)
                ->group('u.user_id')
                ->count();
            $amony=DB::name('user_echarge')
                ->alias('a')
                ->join('users u','a.user_id=u.user_id')
                ->where($where)
                ->group('u.user_id')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach($amony as $key=>$a){
                $amony[$key]['count']=DB('user_echarge')->where('user_id='.$a['user_id'].' and process_type=5')->count();
                $amony[$key]['amount']=(DB('user_echarge')->where('user_id='.$a['user_id'].' and process_type=5')->sum('amount'))*-1;
            }
            $result = array("total" => $total, "rows" => $amony);

            return json($result);
        }
    }
    //简历支出
    public function index4(){

        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where = array();
            $filter=input();
            $filters = json_decode($filter['filter'], true);
            if(!empty($filters['user_id'])){
                $where['u.user_id']=$filters['user_id'];
            }
            if(!empty($filters['user_name'])){
                $where['u.user_name']=$filters['user_name'];
            }
            if(!empty($filters['full_name'])){
                $where['u.full_name']=array('like','%'.$filters['full_name'].'%');
            }
            if(!empty($filters['last_time'])) {
                $date = explode(',', $filters['last_time']);
                foreach($date as &$v){
                    $v = strtotime($v);
                }
                $where['u.last_time'] = array('between', $date);
            }
            $where['process_type']=7;
            $total=DB::name('user_echarge')
                ->alias('a')
                ->join('users u','a.user_id=u.user_id')
                ->where($where)
                ->order($sort, $order)
                ->group('u.user_id')
                ->count();
            $amony=DB::name('user_echarge')
                ->alias('a')
                ->join('users u','a.user_id=u.user_id')
                ->where($where)
                ->group('u.user_id')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach($amony as $key=>$a){
                $amony[$key]['count']=DB('user_echarge')->where('user_id='.$a['user_id'].' and process_type=7')->count();
                $amony[$key]['amount']=(DB('user_echarge')->where('user_id='.$a['user_id'].' and process_type=7')->sum('amount'))*-1;
            }
            $result = array("total" => $total, "rows" => $amony);

            return json($result);
        }
    }
    public function lists($ids){
        $charge=Model('Recharge');
        $row = $charge->where(['user_id' => $ids,'process_type'=>array('in',array('4','6','10'))])->select();
        if (!$row) {
            $this->error('查询信息不存在');
        }
        $type=array('4'=>'简历查看','6'=>'任务悬赏','10'=>'有偿分享');


        foreach($row as $key=>$v){
            $row[$key]['type']=$type[$v['process_type']];
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
}